package com.dc.kylin.rabbitmq.controller;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.StandardCharsets;

/****
 * 生产者
 */

@RestController
public class ProducerController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping(value = "/helloworldSend")
    public Object helloworldSend(String message) throws Exception{

        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
        rabbitTemplate.send("helloworldqueue",new Message(message.getBytes(StandardCharsets.UTF_8),messageProperties));
        return "message send: "+message;
    }


    @GetMapping(value = "/fanoutSend")
    public Object fanoutSend(String message) throws Exception{

        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
        rabbitTemplate.send("fanoutExchange","",new Message(message.getBytes(StandardCharsets.UTF_8),messageProperties));
        return "message send: "+message;
    }


    @GetMapping(value = "/workSend")
    public Object workSend(String message) throws Exception{

        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
        for (int i = 0; i < 10; i++) {
            String message1 = "消息序号 "+ i +message;
            rabbitTemplate.send("work_queue",new Message(message1.getBytes(StandardCharsets.UTF_8),messageProperties));
        }
        return "message send: "+message;
    }


    @GetMapping(value = "/directSend")
    public Object directSend(String routingKey,String message) throws Exception{

        if(null == routingKey){
            routingKey = "directExchange01";
        }

        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
        rabbitTemplate.send("directExchange",routingKey,new Message(message.getBytes(StandardCharsets.UTF_8),messageProperties));
        return "message send: "+message;
    }


    @GetMapping(value = "/topicSend")
    public Object topicSend(String routingKey,String message) throws Exception{

        if(null == routingKey){
            routingKey = "directBind.one";
        }

        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
        rabbitTemplate.send("topicExchange",routingKey,new Message(message.getBytes(StandardCharsets.UTF_8),messageProperties));
        return "message send: "+message;
    }




}
